package com.dubito.composewidgets.ui.bottomnavigation

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.dubito.composewidgets.R
import com.dubito.composewidgets.ui.bottomnavigation.model.BottomNavItem

/**
 * author: LiLiang
 * createTime: 2022/10/5-10:43
 * remark:
 **/

val bottomNavigationItems = listOf(
    BottomNavItem.Home,
    BottomNavItem.Bookmarks,
    BottomNavItem.Notification
)

@Composable
fun BottomNavigationComponent() {
    val navController = rememberNavController()
    Box {
        NavGraph(navHostController = navController)
        val navBackStackEntry by navController.currentBackStackEntryAsState()
        val currentDestination = navBackStackEntry?.destination
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(vertical = 12.dp, horizontal = 12.dp)
                .align(Alignment.BottomEnd),
            horizontalArrangement = Arrangement.SpaceBetween,
        ) {
            bottomNavigationItems.forEach {
                BottomNavigationItem(
                    bottomItem = it,
                    selectedItem = bottomNavigationItems.find { item -> item.route == currentDestination?.route },
                    onClick = { bottomNavItem ->
                        NavigationActions(navController).navigate(route = bottomNavItem.route)
                    }
                )
            }
        }
    }
}

@Composable
fun BottomNavigationItem(
    bottomItem: BottomNavItem,
    selectedItem: BottomNavItem?,
    onClick: (BottomNavItem) -> Unit
) {
    val isSelected = bottomItem == selectedItem
    Row(
        modifier = Modifier
            .height(43.dp)
            .background(
                color = if (isSelected) colorResource(id = R.color.purple_200) else Color.Transparent,
                shape = RoundedCornerShape(22.dp)
            )
            .padding(horizontal = 21.dp)
            .clickable(indication = null, interactionSource = remember {
                MutableInteractionSource()
            }, onClick = { onClick(bottomItem) }),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Center
    ) {
        Icon(
            imageVector = bottomItem.icon,
            contentDescription = "",
            tint = if (isSelected) colorResource(id = R.color.black) else colorResource(id = R.color.gray),
            modifier = Modifier
                .width(20.dp)
                .height(20.dp)
        )
        AnimatedVisibility(visible = isSelected) {
            Text(
                text = bottomItem.title,
                modifier = Modifier
                    .padding(start = 16.dp)
                    .clickable(enabled = false, onClick = {}),
                maxLines = 1
            )
        }
    }
}